﻿using MediatR;
using S.Services.SheepMale.Api.Application.Command.PurchaseCommand;
using S.Services.SheepMale.Domain.PurchaseSales;
using S.Services.SheepMale.Infrastructure;
using System.Security.Claims;

namespace S.Services.SheepMale.Api.Application.CommandHandler.PurchaseCommandHandler
{
    public class PurchaseExamineCommandHandler : IRequestHandler<PurchaseExamineCommand, int>
    {
        private readonly IRepository<EntityPurchase> repository;

        public PurchaseExamineCommandHandler(IRepository<EntityPurchase> repository)
        {
            this.repository = repository;
        }

        public async Task<int> Handle(PurchaseExamineCommand request, CancellationToken cancellationToken)
        {
            string[] ids = request.purId.Split(',');
            List<EntityPurchase> lists = new List<EntityPurchase>();
            foreach (var item in ids)
            {
                var result = await repository.GetAsync(int.Parse(item));
                if (result.ExamineType == false)
                {
                    return -1;
                }
                result.ExamineType = false;
                result.ExamineDate = DateTime.Now;
                result.ExamineBy = "阿星";
                lists.Add(result);
            }
            foreach (var item in lists)
            {
                await repository.UpdateAsync(item);
            }

            return 1;
        }
    }
}
